/*
 *  Copyright 2013 Utkin Dmitry
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef PROTOQTRPC_RPCPROTOCOL_H
#define PROTOQTRPC_RPCPROTOCOL_H

#include <QObject>

class QTcpSocket;

namespace protoqtrpc {

class RpcMessage;
class SocketRpcProtocolPrivate;

/**
 * @brief The SocketRpcProtocol class
 * Implements socket protocol to send and receive messages
 */
class SocketRpcProtocol: public QObject
{
    Q_OBJECT

public:
    /**
     * @brief Constructs SocketRpcProtocol instance
     * @param sock Pointer to socket
     */
    SocketRpcProtocol(QTcpSocket* sock);

    /**
     * @brief Destroys SocketRpcProtocol
     */
    ~SocketRpcProtocol();

public slots:
    /**
     * @brief Slot to send message to connected peer
     * @param msg Message to send
     * @return true message was successfully sent
     */
    bool sendMessage(const protoqtrpc::RpcMessage& msg);

signals:
    /**
     * @brief Message from peer received
     * This signal is emitted when new message is received from peer
     * @param msg Received and parsed message
     */
    void messageReceived(const protoqtrpc::RpcMessage& msg);

private slots:
    void readyRead();

private:
    Q_DECLARE_PRIVATE(SocketRpcProtocol)
    Q_DISABLE_COPY(SocketRpcProtocol)
    SocketRpcProtocolPrivate* d_ptr;
};

}

#endif // PROTOQTRPC_RPCPROTOCOL_H
